<?php

namespace App\Http\Controllers\Backend\Product;

 
use App\Repository\Repositories\Interfaces\ProductBrandsRepository;
use Illuminate\Http\Request;
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
 
 
class BrandController extends BaseController
{
    
    protected $brand_repo;
    
    public function __construct(ProductBrandsRepository $brand_repo)
    {
        $this->brand_repo = $brand_repo;
    }
    
    public function index(Request $request)
    {
          
		$type = $request->input("type","");
       
        if($type == "all"){
            $brands = $this->brand_repo->all();
            return $this->success(ErrorConst::SUCCESS_CODE, $brands,false);
        }else{
            $brands = $this->brand_repo->paginate($this->perPage);
            return $this->success(ErrorConst::SUCCESS_CODE, $brands,true);
        }

    }
    
    public function show($id)
    {
        $brand = $this->brand_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $brand);
    }
    
    public function create(Request $request)
    {
        
        $this->validate($request,[
            'name'=> 'required|string',
            'initial' => 'required|string|size:1',
            'categories'=>'array',
            'sort'=>'integer',
        ]);
        
        $data = $request->all();
        
        if ( isset($data['business_code']) &&  $data['business_code'] ){
            $brand = $this->brand_repo->findByField('business_code', $data['business_code']);
            if(sizeof($brand) > 0 ){
                return $this->error("厂家代码已存在");
            } 
        }
        $data['status']=1;
        $brand = $this->brand_repo->createNew($data);
        if($brand){
            $this->brand_repo->update(['business_code'=>'V'.(11200+$brand['data']['id'])],$brand['data']['id']);
            return $this->success(ErrorConst::SUCCESS_CODE, $brand['data']);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
        
    }
    
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'=> 'required|string',
            'initial' => 'required|string|size:1',
            'sort'=>'integer',
        ]);
        $data = $request->all();
        
        if ( isset($data['business_code']) &&  $data['business_code'] ){
            $brand = $this->brand_repo->findByField('business_code', $data['business_code']);
            if(sizeof($brand) > 0 && $brand['data'][0]['id'] != $id ){
                return $this->error("厂家代码已存在");
            }
        }
        
        $categories = isset($data['categories']) ? $data['categories'] : "";
        if($categories){
            unset($data['categories']);
            $data['copy_category'] = implode(",", $categories);
        }
        
        $ret = $this->brand_repo->update($data, $id);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
    }
    
    public function delete($id)
    {
        $ret = $this->brand_repo->delete($id);
       
        
        if ($ret) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

   
}
